package com.ysu.zyw.service;

import com.ysu.zyw.dao.BlogArticleMapper;
import com.ysu.zyw.dao.BlogCategoryMapper;
import com.ysu.zyw.other.exception.KnownException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;

/**
 * Created by zhang_yao_wu1993 on 15/4/27.
 *
 * @author by Zerur
 */
@Service("blogCategoryService")
public class BlogCategoryService {
    private BlogCategoryMapper blogCategoryMapper;
    private BlogArticleMapper blogArticleMapper;

    public BlogCategoryMapper getBlogCategoryMapper() {
        return blogCategoryMapper;
    }

    @SuppressWarnings("all")
    @Resource
    public void setBlogCategoryMapper(BlogCategoryMapper blogCategoryMapper) {
        this.blogCategoryMapper = blogCategoryMapper;
    }

    public BlogArticleMapper getBlogArticleMapper() {
        return blogArticleMapper;
    }

    @SuppressWarnings("all")
    @Resource
    public void setBlogArticleMapper(BlogArticleMapper blogArticleMapper) {
        this.blogArticleMapper = blogArticleMapper;
    }



    @Transactional
    @RolesAllowed("ROLE_ADMIN")
    public void deleteCategory(String id) {
        Integer articleCount = blogArticleMapper.selectArticleCountByCategoryId(Integer.parseInt(id));
        if(articleCount != 0) {
            throw new KnownException("当前分类下还有文章 请先删除文章");
        }
        blogCategoryMapper.deleteBlogCategory(Integer.parseInt(id));
    }
}
